package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectLabelConverter;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.application.service.SubjectLabelApplicationService;

import com.jingdinajichi.subject.common.entity.Result;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zwy
 * @version 1.0
 * @description: SubjectLabelController题目标签·控制层
 * @date 2025/7/12 16:35
 */
@RestController
@Slf4j
@RequestMapping("subject/label")
public class SubjectLabelController {
    @Resource
    private SubjectLabelApplicationService subjectLabelApplicationService;
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if(log.isInfoEnabled()){
            log.info("SubjectLabelController.add.subjectLabelDTO:{}", JSON.toJSON(subjectLabelDTO));
        }

//            Preconditions.checkNotNull(!StringUtils.isBlank(subjectLabelDTO.getLabelName()),"标签名称不能为空");
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"分类id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelConverter.INSTANCE.convertDTOToBO(subjectLabelDTO);
            int num = subjectLabelApplicationService.add(subjectLabelBO);
            //TODO 调用领域服务
            //subjectLabelApplicationService.add(subjectLabelBO);
            return num == 1 ? Result.ok(true) : Result.fail("插入失败");


    }
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if(log.isInfoEnabled()){
            log.info("SubjectLabelController.update.subjectLabelDTO:{}", JSON.toJSON(subjectLabelDTO));
        }

            Preconditions.checkNotNull(subjectLabelDTO.getId(),"标签id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelConverter.INSTANCE.convertDTOToBO(subjectLabelDTO);
            boolean b = subjectLabelApplicationService.update(subjectLabelBO);
            return  b ? Result.ok(b) : Result.fail("更新失败");


    }
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if(log.isInfoEnabled()){
            log.info("SubjectLabelController.delete.subjectLabelDTO:{}", JSON.toJSON(subjectLabelDTO));
        }

            Preconditions.checkNotNull(subjectLabelDTO.getId(),"标签id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelConverter.INSTANCE.convertDTOToBO(subjectLabelDTO);
            boolean b = subjectLabelApplicationService.delete(subjectLabelBO);
            return  b? Result.ok(b) : Result.fail("删除失败");


    }
    /**
     * 根据分类id查询标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO){
        if(log.isInfoEnabled()){
            log.info("SubjectLabelController.queryLabelByCategoryId.subjectLabelDTO:{}", JSON.toJSON(subjectLabelDTO));
        }

            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"分类id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelConverter.INSTANCE.convertDTOToBO(subjectLabelDTO);
            List<SubjectLabelBO> subjectLabelBOS = subjectLabelApplicationService.queryLabelByCategoryId(subjectLabelBO);
            List<SubjectLabelDTO> subjectLabelDTOS = SubjectLabelConverter.INSTANCE.convertBOToDTO(subjectLabelBOS);
            return Result.ok(subjectLabelDTOS);

    }
}
